package org.example;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class test18 {
    public static void main(String[] args) {
        // 创建一个JavaBean对象
        MyBean bean = new MyBean();
        bean.setProperty1("Value 1");
        bean.setProperty2("Value 2");
        bean.setProperty3("Value 3");
        bean.setProperty4("Value 4");
        bean.setProperty5("Value 5");
        bean.setProperty6("Value 6");
        bean.setProperty7("Value 7");
        bean.setProperty8("Value 8");
        bean.setProperty9("Value 9");
        bean.setProperty10("Value 10");

        // 创建ObjectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            // 忽略未知的属性
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

            // 序列化为JSON字符串
            String json = objectMapper.writeValueAsString(bean);
            System.out.println("json: " + json);

            // 反序列化为JavaBean对象，仅包含需要的属性
            MyBean Bean = objectMapper.readValue(json, MyBean.class);
            System.out.println("JSON: " + Bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Getter
@Setter
@NoArgsConstructor
class MyBean {
    private String property1;
    private String property2;
    private String property3;
    private String property4;
    private String property5;
    private String property6;
    private String property7;
    private String property8;
    @JsonIgnore
    private String property9;
    @JsonIgnore
    private String property10;

}
